<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">


      <el-form-item label="平台单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入平台单号" clearable style="width: 200px;"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商户单号" prop="merchantOrderNo">
        <el-input v-model="queryParams.merchantOrderNo" placeholder="请输入商户单号" clearable style="width: 200px;"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="用户ID" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入用户ID" clearable style="width: 150px;"
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="状态" clearable style="width: 80px">
          <el-option label="支付中" value="handling"></el-option>
          <el-option label="支付成功" value="success"></el-option>
          <el-option label="驳回" value="reject"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"></el-date-picker>
      </el-form-item>

<!--      <el-form-item label="用户状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="操作状态" clearable style="width: 100px">
          <el-option label="待确认" value="paid"></el-option>
          <el-option label="纠纷单" value="has_error"></el-option>
          <el-option label="已完成" value="finish"></el-option>
          <el-option label="已取消" value="cancel"></el-option>
        </el-select>
      </el-form-item> -->
<!--      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 180px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"></el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="success" icon="el-icon-search" size="mini" @click="handleExport">导出订单</el-button>
           <el-button type="success" icon="el-icon-search" size="mini" @click="handleAdd">创建订单</el-button>
      </el-form-item>
    </el-form>
    <div class="text-success">


      今日：¥{{todayAmount}}；昨日：¥{{yestodayAmount}}；当月：¥{{thisMonthAmount}}；上月：¥{{preMonthAmount}}；累计:¥{{totalAmount}}

    </div>

    <br />



    <el-table ref="tables" v-loading="loading" :data="list" @selection-change="handleSelectionChange"
      :default-sort="defaultSort" @sort-change="handleSortChange">
      <!-- <el-table-column type="selection" width="50" align="center" /> -->
      <!-- <el-table-column label="商户号" align="center" prop="merchantId" width="90" /> -->
        <el-table-column label="平台单号" align="center" prop="orderNo" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="商户单号" align="center" prop="merchantOrderNo" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="收款金额" align="center" width="80" prop="amount" :show-overflow-tooltip="true" />
      <<!-- el-table-column label="服务费(U)" align="center" prop="serviceFee" :show-overflow-tooltip="true" />
      <el-table-column label="抢单费(U)" align="center" prop="createFee" :show-overflow-tooltip="true" /> -->
      <el-table-column label="到账金额" align="center" width="80" prop="actualAmount" :show-overflow-tooltip="true"
        :sort-orders="['descending', 'ascending']" />
      <!-- <el-table-column label="状态" align="center" prop="status" :show-overflow-tooltip="true" /> -->
      <!-- <el-table-column label="付款人" align="center" width="100" prop="payer" :show-overflow-tooltip="true" /> -->
      <el-table-column label="订单状态" width="80" align="center">
        <template #default="scope">
          <el-tag type="info" v-if="scope.row.orderStatus == 'handling'">待收款</el-tag>
          <el-tag type="success" v-if="scope.row.orderStatus == 'success'">成功</el-tag>
          <el-tag type="danger" v-if="scope.row.orderStatus == 'reject'">已驳回</el-tag>
        </template>
      </el-table-column>

<!--      <el-table-column label="用户状态" width="80" align="center">
        <template #default="scope">
          <el-tag type="info" v-if="scope.row.status == 'paid'">待确认</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 'timeout'">超时</el-tag>
              <el-tag type="success" v-if="scope.row.status == 'finish'">已到账</el-tag>
        </template>
      </el-table-column> -->

      <el-table-column label="下游返回" width="80" align="center">
        <template #default="scope">
          <el-tag type="success" v-if="scope.row.returnStatus == '1'">已返回</el-tag>
          <el-tag type="danger" v-if="scope.row.returnStatus == '0'">未返回</el-tag>
        </template>
      </el-table-column>



      <!--
      <el-table-column label="用户状态" width="80" align="center">
        <template #default="scope">

          <el-tag type="warning" v-if="scope.row.status == 'paid'">待确认</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 'has_error'">纠纷单</el-tag>
          <el-tag type="success" v-if="scope.row.status == 'finish'">已完成</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 'cancel'">已取消</el-tag>
        </template>
      </el-table-column> -->

      <!-- <el-table-column label="单价(元)" align="center" prop="goodsPrice" width="80" :show-overflow-tooltip="true" /> -->

      <!-- <el-table-column label="人民币(元)" align="center" prop="rmbAmount" :show-overflow-tooltip="true" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" /> -->
      <!--      <el-table-column label="用户ID" width="80" align="center" prop="userId" />
      <el-table-column label="收款人" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="账号" align="center" prop="account" width="200" /> -->
      <!-- <el-table-column label="收款人ID" width="80" align="center" prop="userId" /> -->
      <!-- <el-table-column label="收款人" align="center" prop="username" :show-overflow-tooltip="true" /> -->
      <el-table-column label="通道名称" align="center" prop="channelName"  :show-overflow-tooltip="true" />


      <el-table-column label="创建日期" align="center" prop="createTime" width="150" sortable="custom"
        :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center"  class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--     <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"
            v-hasPermi="['monitor:operlog:query']"
          >详细</el-button> -->

          <el-button type="success" @click="notice(scope.row)">补发通知
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <!-- <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item> -->
        <!-- <el-form-item label="支付宝" prop="account">
          <el-input v-model="form.account" placeholder="请输入支付宝账号" />
        </el-form-item> -->
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="通道" prop="channelCode">
            <el-select
                      v-model="form.channelCode"
                      placeholder="请选择通道"
                      clearable
                      style="width: 240px"
                    >
                      <el-option
                        v-for="dict in channelList"
                        :key="dict.channelCode"
                        :label="dict.channelName"
                        :value="dict.channelCode"
                      />
                    </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    agentList,
    send,
    cancel,
    finish,
    notice,
    add,
    createOrder,
    createOrderByPhone,
    cleanOperlog
  } from "@/api/system/order";
  import {
    channelList

  } from "@/api/system/channel";

  export default {
    name: "Operlog",
    dicts: ['sys_oper_type', 'sys_common_status'],
    data() {
      return {
          channelList:[],
        totalAmount: "0",
        todayAmount: "0",
        yestodayAmount: "0",
        thisMonthAmount: "0",
        preMonthAmount: "0",
        totalAmount: "0",
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        list: [],
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 默认排序
        defaultSort: {
          prop: 'operTime',
          order: 'descending'
        },
        // 表单参数
        form: {},
        title: "",
        // 表单校验
        rules: {
          phone: [{
            required: true,
            message: "手机号不能为空",
            trigger: "blur"
          }],
          // name: [
          //   { required: true, message: "姓名不能为空", trigger: "blur" }
          // ],
          account: [{
            required: true,
            message: "账号不能为空",
            trigger: "blur"
          }],
          amount: [{
            required: true,
            message: "金额不能为空",
            trigger: "blur"
          }]
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: undefined,
          operName: undefined,
          businessType: undefined,
          status: undefined,
         // type: "hand"
        }
      };
    },
    created() {
      this.getList();
       this.finChannels();
    },
    methods: {
      finChannels() {

          channelList().then(response => {
          this.channelList = response.data;

        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.handleQuery() ;

        this.download('agent/order/export', {
          ...this.queryParams
        }, `order_${new Date().getTime()}.xlsx`)
      },
      /** 查询登录日志 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加订单";
      },
      reset() {
        this.form = {
          postId: undefined,
          postCode: undefined,
          postName: undefined,
          postSort: 0,
          status: "0",
          remark: undefined
        };
        this.resetForm("form");
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 提交按钮 */
      submitForm: function() {
        // this.$refs["form"].validate(valid => {
        //   if (valid) {

        createOrder(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          if (response.data.url) {
            window.open(response.data.url)
          }
          this.getList();
        });
        // });
      },
      getList() {
        this.loading = true;
        agentList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.list = response.data.pageResult.list;
          this.total = response.data.pageResult.total;
          this.loading = false;
          this.todayAmount = response.data.sumAmountVo.todayAmount;
          this.yestodayAmount = response.data.sumAmountVo.yestodayAmount;
          this.thisMonthAmount = response.data.sumAmountVo.thisMonthAmount;
          this.preMonthAmount = response.data.sumAmountVo.preMonthAmount;
          this.totalAmount = response.data.sumAmountVo.totalAmount;
        });
      },
      // 操作日志类型字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.dict.type.sys_oper_type, row.businessType);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.queryParams.pageNum = 1;
        this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      },
      /** 多选框选中数据 */
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.operId)
        this.multiple = !selection.length
      },
      /** 排序触发事件 */
      handleSortChange(column, prop, order) {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
        this.getList();
      },
      /** 详细按钮操作 */
      handleView(row) {
        this.open = true;
        this.form = row;
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const operIds = row.operId || this.ids;
        this.$modal.confirm('是否确认删除日志编号为"' + operIds + '"的数据项？').then(function() {
          return delOperlog(operIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 清空按钮操作 */
      handleClean() {
        this.$modal.confirm('是否确认清空所有操作日志数据项？').then(function() {
          return cleanOperlog();
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("清空成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      // handleExport() {
      //   this.download('monitor/operlog/export', {
      //     ...this.queryParams
      //   }, `operlog_${new Date().getTime()}.xlsx`)
      // },
      cancelOrder(row) {
        this.$modal.confirm('是否取消订单').then(function() {
          return cancel(row.id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("成功");
        }).catch(() => {});
      },
      finish(row) {
        this.$modal.confirm('是否确认收款').then(function() {
          return finish(row.id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("成功");
        }).catch(() => {});
      },
      notice(row) {
        notice(row.id);
        this.$modal.msgSuccess("成功");
        this.getList();


      },
      /** 强退按钮操作 */
      handleForceLogout(row) {
        this.$modal.confirm('是否确认金额:' + row.rmbAmount + '已付款？').then(function() {
          return send(row.id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("成功");
        }).catch(() => {});
      }
    }
  };
</script>
